/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-04-07     DRZZZ       the first version
 */
#include "head.h"
#if (CODE == FIRST)
#include <stdlib.h>
#include <rtthread.h>

#include <stdio.h>
#include "board.h"
#include "sensor.h"
#include "sensor_hc_sr04.h"
#include "SR04.h"
#include "uart_lcd.h"

#define LOG_TAG              "SR04"
#define LOG_LVL              LOG_LVL_INFO
#include <ulog.h>

#define SR04_FIRST_1_TRIG_PIN  GET_PIN(D,  5)
#define SR04_FIRST_1_ECHO_PIN  GET_PIN(G, 10)
static struct rt_sensor_config  first_1_cfg;
static rt_base_t  first_1_pins[2] =  {SR04_FIRST_1_TRIG_PIN,
                                    SR04_FIRST_1_ECHO_PIN};

#define SR04_FIRST_2_TRIG_PIN GET_PIN(G,  8)
#define SR04_FIRST_2_ECHO_PIN GET_PIN(A, 12)
static struct rt_sensor_config first_2_cfg;
static rt_base_t first_2_pins[2] =  {SR04_FIRST_2_TRIG_PIN,
                                    SR04_FIRST_2_ECHO_PIN};

#define SR04_FIRST_3_TRIG_PIN GET_PIN(D,  0)
#define SR04_FIRST_3_ECHO_PIN GET_PIN(D,  3)
static struct rt_sensor_config first_3_cfg;
static rt_base_t first_3_pins[2] =  {SR04_FIRST_3_TRIG_PIN,
                                    SR04_FIRST_3_ECHO_PIN};

#define SR04_FIRST_4_TRIG_PIN GET_PIN(B,  5)
#define SR04_FIRST_4_ECHO_PIN GET_PIN(B,  6)
static struct rt_sensor_config first_4_cfg;
static rt_base_t first_4_pins[2] =  {SR04_FIRST_4_TRIG_PIN,
                                    SR04_FIRST_4_ECHO_PIN};

#define SR04_SECOND_TRIG_PIN GET_PIN(E,  0)
#define SR04_SECOND_ECHO_PIN GET_PIN(E,  7)
static struct rt_sensor_config second_cfg;
static rt_base_t second_pins[2] = {SR04_SECOND_TRIG_PIN,
                                   SR04_SECOND_ECHO_PIN};

char  first_level_1 = 0,
      first_level_2 = 0,
      first_level_3 = 0,
      first_level_4 = 0,
       second_level = 0;

char sr04_show_flag = 0;

void sr04_data(void)
{
    if(sr04_show_flag == 0)
        sr04_show_flag = 1;
}

MSH_CMD_EXPORT(sr04_data, show sr04 data);

int SR04_read_distance(void);
int rt_hw_sr04_port(void);
static void sr04_read_distance_entry(void *parameter)
{
    first_1_cfg.intf.dev_name = "timer11";
    first_1_cfg.intf.user_data = (void *)first_1_pins;
    first_2_cfg.intf.dev_name = "timer11";
    first_2_cfg.intf.user_data = (void *)first_2_pins;
    first_3_cfg.intf.dev_name = "timer11";
    first_3_cfg.intf.user_data = (void *)first_3_pins;
    first_4_cfg.intf.dev_name = "timer11";
    first_4_cfg.intf.user_data = (void *)first_4_pins;
     second_cfg.intf.dev_name = "timer11";
     second_cfg.intf.user_data = (void *) second_pins;
    rt_hw_sr04_init("sr04", &first_1_cfg);

    rt_device_t dev = RT_NULL;
    struct rt_sensor_data sensor_data;
    rt_size_t res;

    dev = rt_device_find(parameter);
    if (dev == RT_NULL) {
        rt_kprintf("Can't find device:%s\n", parameter);
        return;
    }

    if (rt_device_open(dev, RT_DEVICE_FLAG_RDWR) != RT_EOK) {
        rt_kprintf("open device failed!\n");
        return;
    }
    rt_device_control(dev, RT_SENSOR_CTRL_SET_ODR, (void *)100);
    while (1) {
        sr04_dev = _sr04_init(&first_1_cfg);
        res = rt_device_read(dev, 0, &sensor_data, 1);
        first_level_1 = sensor_data.data.proximity/10;
//        if(first_level_1 > BOX1_MAX_LEVEL)
//            Set_Peripheral(BOX1_IN_CONTROL, WORK);
//        else
//            Set_Peripheral(BOX1_IN_CONTROL, STOP);
        if(sr04_show_flag == 1)
            rt_kprintf("1DIS:%3d.%dcm\n", sensor_data.data.proximity / 10, sensor_data.data.proximity % 10);
        Tolcd_8bite(0x01,0x11,(char)first_level_1);
        rt_thread_mdelay(100);


        sr04_dev = _sr04_init(&first_2_cfg);
        res = rt_device_read(dev, 0, &sensor_data, 1);
        first_level_2 = sensor_data.data.proximity/10;
//        if(first_level_2 > BOX2_MAX_LEVEL)
//            Set_Peripheral(BOX2_IN_CONTROL, WORK);
//        else
//            Set_Peripheral(BOX2_IN_CONTROL, STOP);
        if(sr04_show_flag == 1)
            rt_kprintf("2DIS:%3d.%dcm\n", sensor_data.data.proximity / 10, sensor_data.data.proximity % 10);
        Tolcd_8bite(0x01,0x21,(char)first_level_2);
        rt_thread_mdelay(100);


        sr04_dev = _sr04_init(&first_3_cfg);
        res = rt_device_read(dev, 0, &sensor_data, 1);
        first_level_3 = sensor_data.data.proximity/10;
//        if(first_level_3 > BOX3_MAX_LEVEL)
//            Set_Peripheral(BOX3_IN_CONTROL, WORK);
//        else
//            Set_Peripheral(BOX3_IN_CONTROL, STOP);
        if(sr04_show_flag == 1)
            rt_kprintf("3DIS:%3d.%dcm\n", sensor_data.data.proximity / 10, sensor_data.data.proximity % 10);
        Tolcd_8bite(0x01,0x31,first_level_3);
        rt_thread_mdelay(100);


        sr04_dev = _sr04_init(&first_4_cfg);
        res = rt_device_read(dev, 0, &sensor_data, 1);
        first_level_4 = sensor_data.data.proximity/10;
//        if(first_level_4 > BOX4_MAX_LEVEL)
//            Set_Peripheral(BOX4_IN_CONTROL, WORK);
//        else
//            Set_Peripheral(BOX4_IN_CONTROL, STOP);
        if(sr04_show_flag == 1)
            rt_kprintf("4DIS:%3d.%dcm\n", sensor_data.data.proximity / 10, sensor_data.data.proximity % 10);
        Tolcd_8bite(0x01,0x41,first_level_4);
        rt_thread_mdelay(100);
    }
}

int SR04_read_distance(void)
{
    rt_thread_t sr04_thread;

    sr04_thread = rt_thread_create("sr04",
                                   sr04_read_distance_entry,
                                   "pr_sr04",
                                   1024,
                                   RT_THREAD_PRIORITY_MAX / 2,
                                   20);
    if (sr04_thread != RT_NULL) {
        rt_thread_startup(sr04_thread);
    }
    LOG_I("--SR04 init over!");
    return RT_EOK;
}

INIT_APP_EXPORT(SR04_read_distance);

#endif
